<template>
  <el-drawer :title="title" :visible.sync="visible" direction="rtl" size="550px" @close="handleClose">
    <el-form ref="detailsForm" label-position="top" label-width="80px" :model="form" :rules="rules">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="公司名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="法人" prop="legalPerson">
            <el-input v-model="form.legalPerson" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="注册资金（万元）" prop="registeredCapital">
            <el-input-number v-model="form.registeredCapital" controls-position="right" :min="1" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属行业" prop="industry">
            <select-dict-tag v-model="form.industry" dict-code="crm_industry" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业规模" prop="scale">
            <select-dict-tag v-model="form.scale" dict-code="crm_scale" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户来源" prop="source">
            <select-dict-tag v-model="form.source" dict-code="crm_source" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业需求" prop="enterprise">
            <select-multi-tag v-model="form.enterprise" dict-code="crm_enterprise_demand" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业上云情况" prop="transportation">
            <select-dict-tag v-model="form.transportation" dict-code="crm_transportation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="成立日期" prop="incorporationDate">
            <el-date-picker v-model="form.incorporationDate" value-format="yyyy-MM-dd" type="date" placeholder="选择日期" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" :rows="4" placeholder="请输入" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="el-drawer__footer">
      <el-button @click="handleClose">关 闭</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-drawer>
</template>

<script>
export default {
  name: 'DetailsDrawer',
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: '',
      visible: this.show,
      loading: false,
      form: {},
      rules: {
        name: [
          { required: true, message: '公司名称不能为空', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '法人不能为空', trigger: 'blur' }
        ],
        registeredCapital: [
          { required: true, message: '注册资金不能为空', trigger: 'blur' }
        ],
        address: [{ required: true, message: '地址不能为空', trigger: 'blur' }],
        industry: [{ required: true, message: '请选择', trigger: 'blur' }],
        scale: [{ required: true, message: '请选择', trigger: 'blur' }],
        source: [{ required: true, message: '请选择', trigger: 'blur' }],
        enterprise: [{ required: true, message: '请选择', trigger: 'blur' }]
      }
    }
  },
  watch: {
    show: {
      immediate: true,
      handler(val) {
        this.visible = this.show
      }
    }
  },
  methods: {
    add() {
      this.title = '新增'
    },
    edit(row) {
      this.title = '编辑'
      this.$nextTick(() => {
        this.form = Object.assign({}, row)
      })
    },
    submitForm() {
      this.$refs.detailsForm.validate((valid) => {
        if (valid) {
          this.handleClose()
        }
      })
    },
    reset() {
      this.form = {
        name: undefined,
        legalPerson: undefined,
        registeredCapital: 0,
        address: undefined,
        industry: undefined,
        scale: undefined,
        source: undefined,
        transportation: undefined,
        incorporationDate: undefined,
        enterprise: [],
        remark: undefined
      }
      this.resetForm('detailsForm')
    },
    handleClose() {
      this.reset()
      this.$emit('update:show', false)
    }
  }
}
</script>
